package com.cma.typhoon.web.credentials;

import com.cma.common.security.MD5Util;
import com.cma.typhoon.web.realm.StatelessToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SaltedAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * Created by bai on 2017/1/8.
 */
public class CustomCredentialMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        StatelessToken statelessToken = (StatelessToken) token;
        String salt =new String(((SaltedAuthenticationInfo)info).getCredentialsSalt().getBytes());

        Object tokenCredentials = encrypt(String.valueOf(statelessToken.getClientDigest()),salt);
        Object accountCredentials = getCredentials(info);
        //将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
        return equals(tokenCredentials, accountCredentials);
    }

    private String encrypt(String password,String salt){
        return MD5Util.getMD5Str(MD5Util.getMD5Str(password) + salt);
    }
}
